package com.uenpay.oem.ui.register;
import android.os.Message;
import android.text.TextUtils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.invokeitems.HttpPostRequest;
import com.uenpay.im.serializations.QYXUserSerializer;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by PS on 2017/6/26.
 */

public class SettingsPresenter implements SettingsContract.Presenter{

    private final SettingsContract.View Mseetingsview;
    private final SettingsPswActivity MseetingsPswActivity;

    public SettingsPresenter(SettingsContract.View view, SettingsPswActivity seetingsPswActivity) {
        Mseetingsview=view;
        this.MseetingsPswActivity=seetingsPswActivity;
    }

    @Override
    public void reglsterSuccess(String userName, final String mobile_phone, final String pwd, final String phone_validate_code) {
        try {
            String url = APIConfiguration.getServiceBaseUrl() + "users/?code=" + phone_validate_code + "&type=mobile";
            JSONObject dataJson = new JSONObject();
            dataJson.put("nickname", userName);
            dataJson.put("mobile", mobile_phone);
            dataJson.put("password", pwd);
            HttpPostRequest.onRequest(url, dataJson.toString(), HttpPostRequest.REQUESTMETHOD_POST, new HttpPostRequest.IRequest() {
                @Override
                public void onResult(int responseCode, String response) {
                    Message msg = MseetingsPswActivity.myHandler.obtainMessage();
                    msg.what = 0;
                    MseetingsPswActivity.myHandler.sendMessage(msg);
                    if (responseCode == 200) {
                        JSONObject js = null;
                        try {
                            js = new JSONObject(response);
                            IMApplication.getInstance().setLoginInfo(js.optString("custid"), js.optString("token"), js.optString("auth"));
                            IMApplication.getInstance().setUcrToken(js.optString("ucrtoken"));
                            IMApplication.getInstance().setUcrOpenId(js.optString("ucropenid"));
                            QYXUserSerializer.deserializeUser(js);

                          Mseetingsview.preparelogin();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } else {
                        if (!TextUtils.isEmpty(response)) {
                            JSONObject j = null;
                            try {
                                j = new JSONObject(response);
                                IMApplication.showToast(j.optString("err_msg"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {

    }
}
